Repository navigation
Stabilize Poisson probabilities, log masses, and inverse-CDF searches - #153
Merged
Merged
Conversation
Retain the direct formula when the exponential factor and computed mass are normal and finite; otherwise use a real-valued logarithmic fallback. Bound factorial work for large counts and avoid wrapping the factorial argument at size_t.max. Add float, double, and real regressions for central overflow, infinite rates, maximum counts, and a subnormal tail probability. AI-Assisted-By: Codex (GPT-6 Astra Medium)
Fall back to incomplete gamma when the direct CDF sum loses range or precision, and bound long direct sums. Convert counts before adding one in gamma calls so size_t.max cannot wrap. Preserve ordinary CCDF subtraction when the remaining tail is at least one eighth; evaluate smaller tails directly with incomplete gamma. Cover central overflow, small upper tails, infinite rates, and maximum counts across float, double, and real. AI-Assisted-By: Codex (GPT-6 Astra Medium)
Use the direct PMF fallback when adjacent complemented incomplete gamma values are too close for reliable subtraction. Convert counts before adding one, and preserve the existing zero-event path. Cover representable upper-tail masses, maximum counts, and agreement across the subtraction and fallback regions for float, double, and real. AI-Assisted-By: Codex (GPT-6 Astra Medium)
Bracket and bisect the count using the smaller probability tail instead of accumulating PMFs. This avoids stalled sums, unsigned downward-loop wraparound, and incorrect results after CDF overflow. Use size_t.max for unbounded or unrepresentable count quantiles, consistent with negative binomial inversion. Handle both endpoints explicitly in count approximations and gamma rate inversion, and convert before incrementing the gamma shape. Add independent quantile references and float, double, and real endpoint and neighboring-bin checks, including probabilities immediately below one. AI-Assisted-By: Codex (GPT-6 Astra Medium)
Halve large rates before evaluating the double-precision exponential, then square in Fp storage. This prevents an underflowed seed from discarding an otherwise representable mass or extended-range zero-event probability. Keep the ordinary exponential path for rates through 512, document its precision limit, and cover central recovery and both sides of the scaling boundaries. AI-Assisted-By: Codex (GPT-6 Astra Medium)
Evaluate large-count log masses with Stirling's correction and a centered deviance term. Near the mean, use a convergent series instead of subtracting large nearly equal logarithmic terms. Reuse this calculation in the PMF fallback and public log PMF. Handle zero counts and infinite rates explicitly, avoid maximum-count factorial overflow, and add independent central references and branch-boundary checks. AI-Assisted-By: Codex (GPT-6 Astra Medium)
The Poisson CDF at count and rate 10000 is approximately 0.5026595812190076, so probability 0.5026596 requires count 10001. Check both sides of this independently verified boundary instead of preserving the old inverse-search result. AI-Assisted-By: Codex (GPT-6 Astra Medium)
Keep the original result-type logarithm and factorial evaluation below count 32, with explicit zero-count and infinite-rate handling. Reserve the centered real-precision expansion for larger counts. This removes the measured Linux LDC slowdown from promoting every small-count log mass to real while retaining the numerical fixes in the large-count path. AI-Assisted-By: Codex (GPT-6 Astra Medium)
Describe both uses of poissonLogMass: the PMF fallback and large-count LPMF evaluation. Remove the outdated statement that it runs only when direct intermediates fail. AI-Assisted-By: Codex (GPT-6 Astra Medium)
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Add regressions for zero-event exponential underflow, an overflowing count-to-rate ratio with a finite log mass, and extended rates beyond the double exponential seed's range. Check finite rates whose quantiles exceed size_t.max and probabilities just above the normal approximation's lower cutoff, where rounding can produce a nonpositive continuity-corrected result. Exercise the six branches reported as uncovered without changing production behavior. AI-Assisted-By: Codex (GPT-6 Astra Medium)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix Poisson calculations that lose representable probabilities through intermediate overflow, underflow, or cancellation, and replace the inverse-CDF search with bounded bracketing and bisection.
For example, the direct PMF at count and rate 200 previously returned NaN instead of approximately 0.0281977. The direct CDF at count and rate 1000 also overflowed, causing the median quantile to return 999 instead of 1000. Small upper tails could disappear when calculated as
1 - CDF.Changes
size_t.max.fp_poissonPMFby halving the rate before evaluating the exponential and squaring in extended storage. Document that the exponential seed still has double precision.p = 0andsize_t.maxatp = 1, also usingsize_t.maxfor results outside the return type's range. Gamma rate inversion retains its distinct endpoint interpretation.Validation
Incomplete-gamma accuracy and cost remain dependent on Phobos. The normal approximation algorithms are not redesigned.
AI-Assisted-By: Codex (GPT-6 Astra Medium)